home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / wiconify / wutilities.lzh / wIconifyScreen / wIconifyScreen.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  7KB  |  259 lines

  1. /*
  2.  *  WICONIFYSCREEN  A companion utility to wIconify that allows you
  3.  *                  to iconify any screen via a CLI command, and modify
  4.  *                  its icon flags.
  5.  *
  6.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  7.  *  You may use this code, provided this copyright notice is kept intact.
  8.  */
  9.  
  10. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  11. #include <intuition/intuitionbase.h>
  12. #include "wIcon.h"
  13.  
  14. #define USAGE\
  15.    "wIconifyScreen [screen] [[-]NOMOVE] [[-]NOSAVEPOS]\n   \
  16.         [[-]NOMULTISELECT] [[-]LOCKED] [[-]NOORGANIZE]"
  17.  
  18. #define DEFAULTFLAGS    WI_NOCLOSE
  19.  
  20. static char *program = "wIconifyScreen";
  21. static char *version = "v1.2";
  22. static char *copyright =
  23.    "Copyright (C) 1990 by Davide P. Cervone, all rights reserved.";
  24.  
  25.  
  26. #define INTUITION_REV   0L
  27. extern struct IntuitionBase *IntuitionBase;
  28. extern struct IntuitionBase *OpenLibrary();
  29.  
  30. static WICON theIcon;               /* Definition of the icon to use */
  31. static ULONG Flags = DEFAULTFLAGS;  /* The flags to use */
  32. static ULONG NoFlags = 0;           /* Have flags been specified? */
  33. static WORD  x,y;                   /* The icon's position */
  34. static char *ScreenName = NULL;     /* the screen's name */
  35.  
  36.  
  37. #define ARGMATCH(s)     (stricmp(*argv,s) == 0)
  38. #define TOUPPER(c)      (((c)>='a'&&(c)<='z')?(c)-'a'+'A':c)
  39. #define NOFLAGSYET\
  40.    (Flags == DEFAULTFLAGS && NoFlags == 0 && x == 0 && y == 0)
  41.  
  42.  
  43.  
  44. /*
  45.  *  Print()
  46.  *
  47.  *  Find the standard AmigaDOS output file and write the output string
  48.  *  to the output file.  This is intended as a substitute for printf()
  49.  *  when no formatting is required, and when you want a small executable.
  50.  */
  51.  
  52. static void Print(s)
  53. char *s;
  54. {
  55.    ULONG OutFile;
  56.    extern ULONG Output();
  57.    
  58.    OutFile = Output();
  59.    if (OutFile && s) Write(OutFile,s,strlen(s));
  60. }
  61.  
  62.  
  63. /*
  64.  *  Error()
  65.  *
  66.  *  Print the character string, and up to two optional strings, then
  67.  *  go to a new output line, close Intuition, and exit with an error.
  68.  */
  69.  
  70. static void Error(s,x1,x2)
  71. char *s,*x1,*x2;
  72. {
  73.    Print(s);
  74.    if (x1) Print(x1);
  75.    if (x2) Print(x2);
  76.    Print("\n");
  77.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  78.    _exit(10L);
  79. }
  80.  
  81.  
  82. /*
  83.  *  ParseArguments()
  84.  *
  85.  *  While there are more arguments to consider,
  86.  *    Move to the next one
  87.  *    If it matches something we're looking for, then add the flag to
  88.  *      the Flags or NoFlags masks.
  89.  *    If the argument is "AT", then
  90.  *      If the next arguement is an X,Y pair, save their values,
  91.  *      Otherwise, give an error message.
  92.  *      Skip the next argument in either case.
  93.  *    If there were no flags and no screen title, then save the arguement
  94.  *       as the screen title.
  95.  *    Otherwise, report an error.
  96.  *  Return TRUE if all parameters were OK, and FALSE otherwise.
  97.  */
  98.  
  99. static int ParseArguments(argc,argv)
  100. int argc;
  101. char **argv;
  102. {
  103.    int status = TRUE;
  104.    int X,Y;
  105.    
  106.    while (--argc)
  107.    {
  108.       argv++;
  109.       if (ARGMATCH("NOMOVE"))        Flags |= WI_NOMOVE; else
  110.       if (ARGMATCH("NOORGANIZE"))    Flags |= WI_NOORGANIZE; else
  111.       if (ARGMATCH("NOSAVEPOS"))     Flags |= WI_NOSAVEPOS; else
  112.       if (ARGMATCH("LOCKED"))        Flags |= WI_LOCKED; else
  113.       if (ARGMATCH("NOMULTISELECT")) Flags |= WI_NOMULTISELECT; else
  114.  
  115.       if (ARGMATCH("-NOMOVE"))        NoFlags |= WI_NOMOVE; else
  116.       if (ARGMATCH("-NOORGANIZE"))    NoFlags |= WI_NOORGANIZE; else
  117.       if (ARGMATCH("-NOSAVEPOS"))     NoFlags |= WI_NOSAVEPOS; else
  118.       if (ARGMATCH("-LOCKED"))        NoFlags |= WI_LOCKED; else
  119.       if (ARGMATCH("-NOMULTISELECT")) NoFlags |= WI_NOMULTISELECT; else
  120.  
  121.       if (ARGMATCH("AT"))
  122.       {
  123.          if (argc > 1 && sscanf(*(argv+1),"%ld,%ld",&X,&Y) == 2)
  124.              x = X, y = Y;
  125.             else
  126.              Print("The AT directive must be of the form 'AT x,y'\n"),
  127.              status = FALSE;
  128.          if (argc > 1) argc--, argv++;
  129.       } else
  130.       if (NOFLAGSYET && ScreenName == NULL) ScreenName = *argv;
  131.       else
  132.       {
  133.          Print("Unrecognized flag '");
  134.          Print(*argv);
  135.          Print("'\n");
  136.          status = FALSE;
  137.       }
  138.    }
  139.    return(status);
  140. }
  141.  
  142.  
  143. /*
  144.  *  PrefixMatch()
  145.  *
  146.  *  Case-insensitive prefix match.  NULL pointers and empty strings
  147.  *  only match themselves, and are not considered prefixes to any string.
  148.  *
  149.  *  Return <0 if the first is smaller than the second,
  150.  *         =0 if the first is a prefix of the second,
  151.  *         >1 if the first is larger than the second.
  152.  */
  153.  
  154. int PrefixMatch(s1,s2)
  155. char *s1,*s2;
  156. {
  157.    int match = -1;
  158.    
  159.    if (s1 && *s1)
  160.    {
  161.       if (s2)
  162.       {
  163.          while (TOUPPER(*s1) == TOUPPER(*s2) && *s2 != 0) s1++,s2++;
  164.          if (*s1 == 0) match = 0; else match = *s1 - *s2;
  165.       } else match = 1;
  166.    } else if (s2 == NULL || *s2 == 0) match = 0;
  167.    return(match);
  168. }
  169.  
  170.  
  171. /*
  172.  *  *FindScreen()
  173.  *
  174.  *  Lock IntuitionBase so nothing happens while we look.
  175.  *  If there is a name to look for,
  176.  *    Start at the first screen, and look for a screen whose title
  177.  *      matches the given name.  If none, return NULL.
  178.  *  Otherwise, use the active screen.
  179.  *  Unlock Intuition.
  180.  *  Return the screen pointer found, if any.
  181.  */
  182.  
  183. static struct Screen *FindScreen(ScreenName)
  184. char *ScreenName;
  185. {
  186.    struct Screen *theScreen;
  187.    long ILock, LockIBase();
  188.  
  189.    ILock = LockIBase(0L);
  190.    if (ScreenName)
  191.    {
  192.       theScreen = IntuitionBase->FirstScreen;
  193.       while (theScreen && PrefixMatch(ScreenName,theScreen->Title))
  194.          theScreen = theScreen->NextScreen;
  195.    } else {
  196.       theScreen = IntuitionBase->ActiveScreen;
  197.    }
  198.    UnlockIBase(ILock);
  199.    return(theScreen);
  200. }
  201.  
  202.  
  203. /*
  204.  *  main()
  205.  *
  206.  *  If all the parameters are OK,
  207.  *  Open Intuition.
  208.  *  Find the specified screen.
  209.  *  If wIconify is runnging
  210.  *    If the screen is iconified, de-iconify it,
  211.  *    Otherwise
  212.  *      Get the screen's icon's current values,
  213.  *      Clear the NoFlags, and add the Flags specified by the user,
  214.  *      If a position was given, set it
  215.  *      Update the icon to the new values.
  216.  *      Iconify the screen.
  217.  *  Otherwise give appropriate errors.
  218.  */
  219.  
  220. void main(argc,argv)
  221. int argc;
  222. char *argv[1];
  223. {
  224.    struct Screen *theScreen = NULL;
  225.    WICONREF *wIconOfScreen();
  226.  
  227.    if (ParseArguments(argc,argv))
  228.    {
  229.       IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
  230.       if (IntuitionBase)
  231.       {
  232.          theScreen = FindScreen(ScreenName);
  233.          if (theScreen)
  234.          {
  235.             if (wIconifyActive())
  236.             {
  237.                if (wIsScreenIconified(theScreen))
  238.                {
  239.                   wRestore(wIconOfScreen(theScreen));
  240.                } else {
  241.                   wGetIconData(&theIcon,wIconOfScreen(theScreen));
  242.                   theIcon.Flags &= ~NoFlags;
  243.                   theIcon.Flags |= Flags;
  244.                   if (x || y)
  245.                   {
  246.                      theIcon.x = x;
  247.                      theIcon.y = y;
  248.                   }
  249.                   wSetScreenIcon(theScreen,&theIcon);
  250.                   wIconifyScreen(theScreen);
  251.                }
  252.             } else Error("wIconify not running or incompatible version",
  253.                           NULL,NULL);
  254.          } else Error("Can't find screen '",ScreenName,"'");
  255.          CloseLibrary(IntuitionBase);
  256.       } else Error("Can't Open Intuition Library!",NULL,NULL);
  257.    } else Error("Usage:  ",USAGE);
  258. }
  259.